package com.xbongbong.paas.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.FormTitlePoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.BomBillEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.saas.enums.business.CommunicatePlanEnum;
import com.xbongbong.saas.enums.business.CompetitorEnum;
import com.xbongbong.saas.enums.business.ContactEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.business.CustomerCommunicateEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.business.MarketActivityEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.enums.business.ProductionOrderEnum;
import com.xbongbong.saas.enums.business.PurchaseEnum;
import com.xbongbong.saas.enums.business.QuotationEnum;
import com.xbongbong.saas.enums.business.RefundEnum;
import com.xbongbong.saas.enums.business.ReturnedPurchaseEnum;
import com.xbongbong.saas.enums.business.SalesOpportunityEnum;
import com.xbongbong.saas.enums.business.SupplierCommunicateEnum;
import com.xbongbong.saas.enums.business.SupplierContactEnum;
import com.xbongbong.saas.enums.business.SupplierEnum;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author 吴峰
 * @date 2022/01/05 15:31
 */
@Slf4j
public class TitleUtil {

    public static String getName(Integer businessType, JSONObject data)throws XbbException{
        return getName(XbbRefTypeEnum.getByCode(businessType),data);
    }

    public static String getName(XbbRefTypeEnum xbbRefTypeEnum, JSONObject data) throws XbbException{
        String attr;
        switch (xbbRefTypeEnum) {
            case CUSTOMER_MANAGEMENT:
                attr = CustomerManagementEnum.NAME.getAttr();
                break;
            case CONTACT:
                attr = ContactEnum.NAME.getAttr();
                break;
            case CUSTOMER_COMMUNICATE:
                attr = CustomerCommunicateEnum.CUSTOMER_NAME_LINK_TEXT.getAttr();
                break;
            case CONTRACT:
                attr = ContractEnum.NAME.getAttr();
                break;
            case SALES_OPPORTUNITY:
                attr = SalesOpportunityEnum.NAME.getAttr();
                break;
            case REFUND:
                attr = RefundEnum.REFUND_NAME.getAttr();
                break;
            case QUOTATION:
                attr = QuotationEnum.NAME.getAttr();
                break;
            case COMMUNICATE_PLAN:
                attr = CommunicatePlanEnum.NAME.getAttr();
                break;
            case PRODUCT:
                attr = ProductEnum.NAME.getAttr();
                break;
            case PURCHASE:
                attr = PurchaseEnum.PURCHASE_NAME.getAttr();
                break;
            case SUPPLIER_CONTACT:
                attr = SupplierContactEnum.NAME.getAttr();
                break;
            case SUPPLIER_COMMUNICATE:
                attr = SupplierCommunicateEnum.SUPPLIER_NAME_LINK_TEXT.getAttr();
                break;
            case RETURNED_PURCHASE:
                attr = ReturnedPurchaseEnum.PURCHASE_SHEET_ID_LINKED_TEXT.getAttr();
                break;
            case SUPPLIER:
                attr = SupplierEnum.SUPPLIER_NAME.getAttr();
                break;
            case BOM_BILL:
                attr = BomBillEnum.NAME.getAttr();
                break;
            case MARKET_ACTIVITY:
                attr = MarketActivityEnum.NAME.getAttr();
                break;
            case CLUE:
                attr = ClueEnum.COMPANY_NAME.getAttr();
                break;
            case COMPETITOR:
                attr = CompetitorEnum.NAME.getAttr();
                break;
            case PRODUCTION_ORDER:
                attr = ProductionOrderEnum.SHEET_NO.getAttr();
                break;
            case WORK_ORDER_V2:
            case RECEIPT_ORDER:
            case SERVICE_PROJECT:
                attr= FieldTypeEnum.SERIALNO.getAlias();
                break;
            default:
                attr = null;
                break;
        }
        return FastJsonHelper.getStringOrDefaultFromFormData(data, attr, "");
    }

    /**
     * 动态获取title
     * date: 2022/6/21 9:52 上午
     * @author yingjf
     * @param businessType:
     * @param paasFormEntityExt:
     * @param explainMap:
     * @param paasFormDataEntityExt:
     */
    public static String getTitle(Integer businessType, PaasFormEntityExt paasFormEntityExt, Map<String, FieldAttrEntity> explainMap,
                            PaasFormDataEntityExt paasFormDataEntityExt){

        String title = "";
        try {
            List<FieldAttrEntity> list = new ArrayList<>();
            Set<Map.Entry<String, FieldAttrEntity>> entrySet = explainMap.entrySet();
            for(Map.Entry<String, FieldAttrEntity> remp:entrySet ){
                FieldAttrEntity fieldAttrEntity = remp.getValue();
                list.add(fieldAttrEntity);
            }
            List<FormTitlePoJo> formTitleList = ExplainUtil.getFormTitle(JSONArray.parseArray(paasFormEntityExt.getCustomTitle()),
                    explainMap, paasFormDataEntityExt.getData(), list, paasFormDataEntityExt);
            if(businessType!=null && businessType!=0){
                title = getName(businessType,paasFormDataEntityExt.getData());
            }else{
                if(!formTitleList.isEmpty()){
                    formTitleList.forEach(titlePoJo->{
                        FieldAttrEntity fieldAttrEntity = explainMap.get(titlePoJo.getAttr());
                        if (Objects.nonNull(fieldAttrEntity) && ExplainUtil.isCombo(fieldAttrEntity.getFieldType())) {
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put(titlePoJo.getAttr(), titlePoJo.getValue());
                            Object value = ItemUtil.analysisComboReturnStr(jsonObject, fieldAttrEntity);
                            titlePoJo.setValue(Collections.singletonList(value));
                        }
                    });
                    title = getTitle(formTitleList);
                }
            }
        }catch (XbbException e){
            log.error("com.xbongbong.paas.service.dynamic.help.DynamicHelp.getTitle", e);
        }
        return title;
    }
    /**
     * 标题拼装
     * @param formTitleList
     * @return
     */
    public static String getTitle(List<FormTitlePoJo> formTitleList) {
        String title = "";
        for (FormTitlePoJo item : formTitleList) {
            if (StringUtil.isEmpty(item.getAttr())) {
                title += item.getContent();
            } else {
                for (Object object : item.getValue()) {
                    title += object.toString();
                }
            }
        }
        return title;
    }

    /**
     * pc端标题
     * @param formTitleList
     * @return
     */
    public static String getWebTitle(List<FormTitlePoJo> formTitleList) {
        StringBuilder title = new StringBuilder();
        for (FormTitlePoJo item : formTitleList) {
            title.append(item.getContent());
            title.append(" ");
            for (Object object : item.getValue()) {
                title.append(object.toString());
                title.append(" ");
            }
        }
        return title.toString();
    }

    /**
     * description:  paas表单获取title  日志显示使用
     * date: 2021/12/22 4:47 下午
     *
     * @Author yingjf
     */
    public static String getPaasFormTitle(PaasFormDataEntityExt paasFormDataEntityExt, PaasFormEntityExt paasFormEntityExt, Map<String, FieldAttrEntity> explainMap) throws XbbException {
        List<FieldAttrEntity> list = new ArrayList<>();
        Iterator<String> iterator = explainMap.keySet().iterator();
        while (iterator.hasNext()) {
            String attr = iterator.next();
            FieldAttrEntity fieldAttrEntity = explainMap.get(attr);
            list.add(fieldAttrEntity);
        }
        List<FormTitlePoJo> formTitleList = ExplainUtil.getFormTitle(JSONArray.parseArray(paasFormEntityExt.getCustomTitle()), explainMap, paasFormDataEntityExt.getData(), list, paasFormDataEntityExt);
        String title = "";
        if (!formTitleList.isEmpty()) {
            formTitleList.forEach(titlePoJo -> {
                FieldAttrEntity fieldAttrEntity = explainMap.get(titlePoJo.getAttr());
                if (Objects.nonNull(fieldAttrEntity) && ExplainUtil.isCombo(fieldAttrEntity.getFieldType())) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put(titlePoJo.getAttr(), titlePoJo.getValue());
                    Object value = ItemUtil.analysisComboReturnStr(jsonObject, fieldAttrEntity);
                    titlePoJo.setValue(Collections.singletonList(value));
                }
            });
            title = TitleUtil.getTitle(formTitleList);
        }
        if (StringUtil.isEmpty(title)) {
            title = "--";
        }
        return title;
    }
}
